package com.example.forum.services.impl;

import com.example.forum.common.AppResult;
import com.example.forum.common.enums.ResultCode;
import com.example.forum.common.validation.ValidationUtils;
import com.example.forum.dao.BoardMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Board;
import com.example.forum.services.IBoardService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author hanzishuai
 * @date 2025/4/9 17:13
 * @Description
 */
@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {

    @Resource
    private BoardMapper boardMapper;

    @Override
    public List<Board> selectByNum(Integer num) {
        // 非空校验
        if (num <= 0) {
            // 打日志，抛异常
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        // 调用 DAO 查询数据库中的数据
        List<Board> result = boardMapper.selectByNum(num);

        return result;
    }

    @Override
    public Board selectById(Long id) {
        ValidationUtils.id(id, "板块 Id");


        // 查询数据库
        Board board = boardMapper.selectByPrimaryKey(id);

        return board;
    }

    @Override
    public void addOneArticleCountById(Long id) {
        ValidationUtils.id(id, "板块 Id");


        // 根据 id 查询对应的板块
        Board board = boardMapper.selectByPrimaryKey(id);

        ValidationUtils.exists(board);
        ValidationUtils.banned(board);

        // 更新帖子数量
        Board updateBoard = new Board();
        updateBoard.setId(id);
        updateBoard.setArticleCount(board.getArticleCount() + 1);

        // 更新数据库
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);

        ValidationUtils.singleRowAffected(row);
    }

    @Override
    public void subOneArticleCountById(Long id) {
        ValidationUtils.id(id, "板块 Id");

        // 根据 id 查询对应的板块
        Board board = boardMapper.selectByPrimaryKey(id);
        ValidationUtils.exists(board);
        ValidationUtils.banned(board);

        // 更新帖子数量
        Board updateBoard = new Board();
        updateBoard.setId(id);
        updateBoard.setArticleCount(board.getArticleCount() - 1);
        // 判断减一之后板块数是否正常
        if (updateBoard.getArticleCount() < 0) {
            updateBoard.setArticleCount(0);
        }

        // 更新数据库
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);

        ValidationUtils.singleRowAffected(row);

    }


    @Override
    public void update(Board board) {
        ValidationUtils.id(board.getId(), "板块 Id");

        int row = boardMapper.updateByPrimaryKeySelective(board);

        ValidationUtils.singleRowAffected(row);
    }

    @Override
    public void create(Board board) {
        if (board == null || board.getName() == null || board.getSort() == null
                || board.getArticleCount() == null || board.getState() == null
                || board.getDeleteState() == null || board.getCreateTime() == null
                || board.getUpdateTime() == null || board.getSort() < 0
                || !StringUtils.hasLength(board.getName())) {
            log.warn("{}", ResultCode.FAILED_PARAMS_VALIDATE);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        int row = boardMapper.insertSelective(board);
        ValidationUtils.singleRowAffected(row);

    }


    public List<Board> selectByName(String name) {
        // 校验
        if(!StringUtils.hasLength(name)) {
            log.warn("{}", ResultCode.FAILED_PARAMS_VALIDATE);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        // 调用 DAO
        List<Board> boards = boardMapper.selectByName(name);

        if(boards == null) {
            log.warn("{}", ResultCode.ERROR_SERVICES);
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }

        return boards;

    }



}





















